<?php
declare(strict_types=1);
namespace cms\office\admin\mvc_controller;

use cms\office\service\ProjectService;
use cms\office\service\RewardService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

/**
 * Class AppController
 * @package cms\office\admin\mvc_controller
 * @AutoController(prefix="mvc/office/admin_reward")
 */
class RewardController extends CommonController
{
    /**
     * @Inject()
     * @var ProjectService
     */
    protected ProjectService $projectService;
    /**
     * @Inject()
     * @var RewardService
     */
    protected RewardService $rewardService;
    /**
     * @return ResponseInterface
     */
    public function retrieve(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $project_id = (int) $this->request->input('project_id', 0);
        $project = $this->projectService->getDataById($project_id);
        return $this->template([
            'app' => $app,
            'project' => $project
        ]);
    }

    /**
     * @return ResponseInterface
     */
    public function create(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $project_id = (int) $this->request->input('project_id', 0);
        $project = $this->projectService->getDataById($project_id);
        return $this->template([
            'app' => $app,
            'project' => $project
        ], 'form');
    }

    /**
     * @return ResponseInterface
     */
    public function update(): ResponseInterface
    {
        $id = (int) $this->request->input('id', 0);
        $data = $this->rewardService->getDataById($id, true);
        return $this->template(['app' => $data->app, 'data' => $data], 'form');
    }

    /**
     * @return ResponseInterface
     */
    public function select(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $select_multi = (int) $this->request->input('select_multi', 0);
        $select_value = (string) $this->request->input('select_value', '');
        // region 处理选中值 +++++
        if ($select_value) {
            if (is_numeric($select_value)) {
                $select_value = [$select_value];
            } elseif (strpos($select_value, ',') !== false) {
                $select_value = explode(',', $select_value);
                foreach ($select_value as $k => $v) {
                    if (!is_numeric($v)) {
                        unset($select_value[$k]);
                    }
                }
            }
            if (!is_array($select_value)) {
                $select_value = [];
            }
            if (!$select_multi && count($select_value) > 1) {
                $select_value = [$select_value[0]];
            }
        } else {
            $select_value = [];
        }
        // endregion
        return $this->template([
            'app' => $app,
            'select_multi' => $select_multi,
            'select_value' => $select_value
        ]);
    }

    /**
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $id = (int) $this->request->input('id', 0);
        $data = $this->rewardService->getDataById($id, true);
        return $this->template([
            'app' => $data->app,
            'data' => $data
        ]);
    }
}